﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_C_Customers
    {
        public string Add(IEnumerable<T_C_Customers> add)
        {
            string addXmL = EFHelp<T_C_Customers>.GetXML(add);
            SqlParameter[] para = { 
                                  new SqlParameter("@addXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXmL;
            para[1].Value = null;
            para[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveCustomer", para);
            return (para[2].Value ?? string.Empty).ToString();
        }

        public string Save(IEnumerable<T_C_Customers> edit)
        {
            string editXmL = EFHelp<T_C_Customers>.GetXML(edit);
            SqlParameter[] para = { 
                                  new SqlParameter("@editXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = editXmL;
            para[1].Value = null;
            para[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveCustomer_Edit", para);
            return (para[2].Value ?? string.Empty).ToString();
        }

        public T_Base<T_C_Customers> Get(string xml)
        {
            T_Base<T_C_Customers> list = new T_Base<T_C_Customers>();
            SqlParameter[] para = { 
                                  new SqlParameter("@para",SqlDbType.Xml,-1)
                                  };
            para[0].Value = xml;
            #region 获取数据
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetCustomer", para))
            {
                List<T_C_Customers> inner = new List<T_C_Customers>();
                if (dr.Read() && dr.HasRows)
                    list.total = dr.GetInt32(0);
                if (dr.NextResult())
                {
                    while (dr.Read() && dr.HasRows)
                    {
                        T_C_Customers dto = new T_C_Customers();
                        dto.CustomerID = dr.GetGuid(0);
                        dto.CustomerCode = dr.GetString(1);
                        dto.CustomerName = dr.GetString(2);
                        dto.MiniCode = dr.GetValue(3) is DBNull ? null : dr.GetString(3);
                        dto.CPhoneNum = dr.GetValue(4) is DBNull ? null : dr.GetString(4);
                        dto.CHandPhone1 = dr.GetValue(5) is DBNull ? null : dr.GetString(5);
                        dto.CHandPhone2 = dr.GetValue(6) is DBNull ? null : dr.GetString(6);
                        dto.CWexin = dr.GetValue(7) is DBNull ? null : dr.GetString(7);
                        dto.CQQ = dr.GetValue(8) is DBNull ? null : dr.GetString(8);
                        dto.CFax = dr.GetValue(9) is DBNull ? null : dr.GetString(9);
                        dto.CEmail = dr.GetValue(10) is DBNull ? null : dr.GetString(10);
                        dto.CAddress = dr.GetValue(11) is DBNull ? null : dr.GetString(11);
                        dto.CLawyer = dr.GetValue(12) is DBNull ? null : dr.GetString(12);
                        dto.CFM = dr.GetValue(13) is DBNull ? null : dr.GetString(13);
                        dto.CBM = dr.GetValue(14) is DBNull ? null : dr.GetString(14);
                        dto.CType = dr.GetValue(15) is DBNull ? null : dr.GetGuid(15).ToString();
                        dto.CPayType = dr.GetValue(16) is DBNull ? null : dr.GetGuid(16).ToString();
                        dto.CLevel = dr.GetValue(17) is DBNull ? null : dr.GetGuid(17).ToString();
                        dto.CreditLimitM = dr.GetValue(18) is DBNull ? null : dr.GetDouble(18).ToString();
                        dto.CreditLimitD = dr.GetValue(19) is DBNull ? null : dr.GetDouble(19).ToString();
                        dto.IsServiceFee = dr.GetBoolean(20);
                        dto.CScore = dr.GetValue(21) is DBNull ? null : dr.GetDouble(21).ToString();
                        // dto.CBankAccount = dr.GetValue(22) is DBNull ? null : dr.GetString(22);
                        dto.CBankName = dr.GetValue(22) is DBNull ? null : dr.GetString(22);
                        dto.CBankCardNo = dr.GetValue(23) is DBNull ? null : dr.GetString(23);
                        dto.CreateBy = dr.GetValue(24) is DBNull ? null : dr.GetString(24);
                        dto.CreateDate = dr.GetDateTime(25);
                        dto.Note = dr.GetValue(26) is DBNull ? null : dr.GetString(26);
                        inner.Add(dto);
                    }
                    list.data = inner;
                }
            }
            #endregion
            return list;
        }

        public string Del(IEnumerable<T_C_Customers> del)
        {
            string delXmL = EFHelp<T_C_Customers>.GetXML(del);
            SqlParameter[] para = { 
                                  new SqlParameter("@delXmL",SqlDbType.Xml,-1),
                                  new SqlParameter("@userXML",SqlDbType.Xml,-1),
                                  new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = delXmL;
            para[1].Value = null;
            para[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_SaveCustomer_Del", para);
            return (para[2].Value ?? string.Empty).ToString();
        }

        public T_C_Customers GetOne(string xml)
        {
            T_C_Customers dto = new T_C_Customers();
            SqlParameter[] para = { 
                                  new SqlParameter("@CustomerID",SqlDbType.UniqueIdentifier,16)
                                  };
            para[0].Value = Guid.Parse(xml);
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, @"P_B_GetCustomer_ID", para))
            {
                while (dr.Read() && dr.HasRows)
                {
                    dto.CustomerID = dr.GetGuid(0);
                    dto.CustomerCode = dr.GetString(1);
                    dto.CustomerName = dr.GetString(2);
                    dto.MiniCode = dr.GetValue(3) is DBNull ? null : dr.GetString(3);
                    dto.CPhoneNum = dr.GetValue(4) is DBNull ? null : dr.GetString(4);
                    dto.CHandPhone1 = dr.GetValue(5) is DBNull ? null : dr.GetString(5);
                    dto.CHandPhone2 = dr.GetValue(6) is DBNull ? null : dr.GetString(6);
                    dto.CWexin = dr.GetValue(7) is DBNull ? null : dr.GetString(7);
                    dto.CQQ = dr.GetValue(8) is DBNull ? null : dr.GetString(8);
                    dto.CFax = dr.GetValue(9) is DBNull ? null : dr.GetString(9);
                    dto.CEmail = dr.GetValue(10) is DBNull ? null : dr.GetString(10);
                    dto.CAddress = dr.GetValue(11) is DBNull ? null : dr.GetString(11);
                    dto.CLawyer = dr.GetValue(12) is DBNull ? null : dr.GetString(12);
                    dto.CFM = dr.GetValue(13) is DBNull ? null : dr.GetString(13);
                    dto.CBM = dr.GetValue(14) is DBNull ? null : dr.GetString(14);
                    dto.CType = dr.GetValue(15) is DBNull ? null : dr.GetGuid(15).ToString();
                    dto.CPayType = dr.GetValue(16) is DBNull ? null : dr.GetGuid(16).ToString();
                    dto.CLevel = dr.GetValue(17) is DBNull ? null : dr.GetGuid(17).ToString();
                    dto.CreditLimitM = dr.GetValue(18) is DBNull ? null : dr.GetDouble(18).ToString();
                    dto.CreditLimitD = dr.GetValue(19) is DBNull ? null : dr.GetDouble(19).ToString();
                    dto.IsServiceFee = dr.GetBoolean(20);
                    dto.CScore = dr.GetValue(21) is DBNull ? null : dr.GetDouble(21).ToString();
                    //dto.CBankAccount = dr.GetValue(22) is DBNull ? null : dr.GetString(22);
                    dto.CBankName = dr.GetValue(22) is DBNull ? null : dr.GetString(22);
                    dto.CBankCardNo = dr.GetValue(23) is DBNull ? null : dr.GetString(23);
                    dto.CreateBy = dr.GetValue(24) is DBNull ? null : dr.GetString(24);
                    dto.CreateDate = dr.GetDateTime(25);
                    dto.Note = dr.GetValue(26) is DBNull ? null : dr.GetString(26);
                }
            }
            return dto;
        }
    }
}
